package com.ruoyi.web.controller.erp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Quotation;
import com.ruoyi.system.domain.QuotationItems;
import com.ruoyi.system.service.IQuotationService;
import com.ruoyi.system.service.IProgressTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 报价单Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/erp/quotation")
public class QuotationController extends BaseController
{
    @Autowired
    private IQuotationService quotationService;

    @Autowired
    private IProgressTemplateService progressTemplateService;

    /**
     * 查询报价单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Quotation quotation)
    {
        startPage();
        List<Quotation> list = quotationService.selectQuotationList(quotation);
        return getDataTable(list);
    }

    /**
     * 获取报价单详细信息
     */
    @GetMapping(value = "/{quotationId}")
    public AjaxResult getInfo(@PathVariable("quotationId") Long quotationId)
    {
        return success(quotationService.selectQuotationById(quotationId));
    }

    /**
     * 新增报价单
     */
    @Log(title = "报价单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Quotation quotation)
    {
        quotation.setCreatedBy(SecurityUtils.getUserId());
        return toAjax(quotationService.insertQuotation(quotation));
    }

    /**
     * 新增报价单明细
     */
    @Log(title = "报价单管理", businessType = BusinessType.INSERT)
    @PostMapping("/items")
    public AjaxResult addItems(@RequestBody QuotationItems item)
    {
        return toAjax(quotationService.insertQuotationItems(item));
    }

    /**
     * 批量新增报价单明细
     */
    @Log(title = "报价单管理", businessType = BusinessType.INSERT)
    @PostMapping("/batchItems")
    public AjaxResult batchAddItems(@RequestBody List<QuotationItems> items)
    {
        return toAjax(quotationService.batchInsertQuotationItems(items));
    }

    /**
     * 修改报价单
     */
    @Log(title = "报价单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Quotation quotation)
    {
        return toAjax(quotationService.updateQuotation(quotation));
    }

    /**
     * 删除报价单
     */
    @Log(title = "报价单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{quotationIds}")
    public AjaxResult remove(@PathVariable Long[] quotationIds)
    {
        return toAjax(quotationService.deleteQuotationByIds(quotationIds));
    }

    /**
     * 获取报价单进度
     */
    @GetMapping("/progress/{quotationId}")
    public AjaxResult getProgress(@PathVariable Long quotationId) {
        Map<String, Object> progress = progressTemplateService.getOrderProgress(quotationId, "报价单");
        return AjaxResult.success(progress);
    }

    /**
     * 更新报价单状态
     */
    @PutMapping("/status/{quotationId}")
    public AjaxResult updateStatus(
        @PathVariable Long quotationId,
        @RequestParam String status)
    {
        Quotation quotation = new Quotation();
        quotation.setQuotationId(quotationId);
        quotation.setStatus(status);
        return toAjax(quotationService.updateQuotation(quotation));
    }
} 